<?php

namespace haibao\app\data;

class ArticleVote extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\ArticleVote');
	}
    
	public function getVoteById($voteId){
	     return $this->getOneById($voteId);
	}
	
	/**
	 * 添加(修改)文章投票选项
	 */
	public function updateArticleVote($articleId,$arrSet){
		$sql = "select * from ArticleVote where ArticleId = %s";
		$result = $this->query($sql, array($articleId));
		$articleList = array();
		$list = array();
		while($result->fetch_assoc()){
			$list['ArticleId'] = $result->getData('ArticleId');
			$list['Content'] = $result->getData('Content');
			$articleList[] = $list;
		}

		if($articleList != $arrSet){
			$sql = "delete from ArticleVote  where ArticleId = %s";
			$this->query($sql,array($articleId));
			foreach($arrSet as $k=>$v){	
				$sql = 'insert into ArticleVote(ArticleId,Content) values(%s,%s)';
				$valArr = array();
				array_push($valArr,$v['ArticleId']);
				array_push($valArr,$v['Content']);
				$this->query($sql,$valArr);
			}
		}
	}
	
	/**
	 * 通过ID得到投票选项
	 * @param unknown $articleId
	 * @return array $voteList;
	 */
	public function getVoteByArticleId($articleId){
		$voteList = array();
		$sql = "select * from ArticleVote where ArticleId=%s";
		$result = $this->query($sql,array($articleId));
		$list = array();
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['ArticleId'] = $result->getData('ArticleId');
			$list['Content'] = $result->getData('Content');
			$list['VoteNum']  = $result->getData('VoteNum');
			$voteList[] = $list;
		}
		return $voteList;
	}
	
	/**
	 * 文章投票列表
	 * @param int $articleId
	 * @param int $userId
	 */
	public function getVoteListByArticleId($articleId){
		$resultArr = $this->getVoteListNumByArticleId($articleId);
		return $resultArr;
	}
	
	//APP接口调用
	/**
	 * 用户对文章是否投票，如果有，返回投票项记录
	 * @param int $articleId
	 * @param int $userId
	 */
	public function getVoteListByArticleIdAndUserId($articleId,$userId){
		$resultArr = array('voteList'=>array(),'voteNum'=>0);
		$userHasVote = 0;
		$sql = "select * from UserVoteLog where ArticleId =%s and UserId=%s";
		$result = $this->query($sql,array($articleId,$userId));
		while($result->fetch_assoc()){
			$userHasVote = 1;
			break;
		}
		if($userHasVote){
			$resultArr = $this->getVoteListNumByArticleId($articleId);
		}
		$resultArr['hasVote'] = $userHasVote;
		return $resultArr;
	}
	
	/**
	 * 用户添加投票
	 * @param unknown_type $articleId
	 * @param unknown_type $userId
	 * @param unknown_type $voteId
	 */
	public function addUserVote($articleId,$userId,$voteId){
		$sql = 'insert into UserVoteLog(ArticleId,UserId,VoteId,CreateTime) values(%s,%s,%s,%s)';
		$this->query($sql,array($articleId,$userId,$voteId,date('Y-m-d H:i:s',time())));
		
		$sql = 'update ArticleVote set VoteNum = VoteNum + 1 where Id=%s and ArticleId=%s';
		$this->query($sql,array($voteId,$articleId));
		
		$sql = 'update ArticleProperty set VoteNum = VoteNum + 1 where ArticleId=%s';
		$this->query($sql, array($articleId));
		
		return $this->getVoteListNumByArticleId($articleId);
	}

	private function getVoteListNumByArticleId($articleId){
		$voteList = array();
		$voteNum = 0;
		$sql = "select Id,Content,VoteNum from ArticleVote where ArticleId=%s";
		$result = $this->query($sql,array($articleId));
		$list = array();
		while($result->fetch_assoc()){
			$list['voteId'] = $result->getData('Id');
			$list['content']  = $result->getData('Content');
			$list['num']  = $result->getData('VoteNum');
			if($result->getData('VoteNum')){
				$voteNum += (int)$result->getData('VoteNum');
			}
			$voteList[] = $list;
		}
		return array('voteList'=>$voteList,'voteNum'=>$voteNum);
	}
	
}